package com.faithbj.utils;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.Random;

import static com.google.common.base.Preconditions.checkArgument;

/**
 * User: xiongyuanwei
 * Date: 12-7-29
 * Time: 下午4:01
 */
public class RandomUtils {

    public static <T> List<T> selectN(List<T> data, int n) {
        checkArgument(n >= 0);
        List<T> copy = Lists.newArrayList(data);
        List<T> ret = Lists.newArrayList();
        if (n == 0) return ret;
        if (n >= data.size()) return copy;
        Random r = new Random();
        for (int i = 0; i < n; i++) {
            int a = r.nextInt(n);
            ret.add(copy.get(a));
            copy.remove(a);
        }
        return ret;
    }

    public static <T> List<T> selectPercentN(List<T> data, int n) {
        checkArgument(n >= 0 && n <= 100);
        int m = ((Number) Math.round(data.size() * n / 100.0)).intValue();
        return selectN(data, m);
    }
}
